#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>

void handler(int signo)
{
    pid_t id;
    while((id = waitpid(-1, NULL, WNOHANG)) > 0)// 等待任一子进程
    {
        std::cout << "wait child process successfully." << std::endl;
    }

    std::cout << "child process has quitted." << std::endl;
}

int main()
{
    signal(SIGCHLD, handler);

    pid_t id = fork();
    if(id == 0)
    {
        // child
        printf("child process: %d, father process: %d\n", getpid(), getppid());
        sleep(3);

        exit(2);
    }
    // father
    while (true)
    {
        /* code */
        std::cout << "father process is doing something ..." << std::endl;

        sleep(1);
    }
    

    return 0;
}